package com.client.ryt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.client.ryt.R;
import com.client.ryt.basic.BaseActivity;
import com.client.ryt.basic.BaseException;
import com.client.ryt.basic.BaseLiteners;
import com.client.ryt.basic.BaseResponse;
import com.client.ryt.basic.BaseTask;
import com.client.ryt.config.LoginConfig;
import com.client.ryt.entity.parameter.LoginParameters;
import com.client.ryt.task.http.request.LoginRequest;
import com.client.ryt.task.http.response.LoginResponse;

public class LoginActivity extends BaseActivity {

    EditText etUserName = null;
    EditText etPassword = null;
    Button btnLogin = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        btnLogin = (Button)findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                etUserName = (EditText)findViewById(R.id.et_username);
                etPassword = (EditText)findViewById(R.id.et_password);
                String userName = etUserName.getText().toString();
                String passWord = etPassword.getText().toString();
                LoginParameters loginParameters = new LoginParameters();
                loginParameters.setUsername(userName);
                loginParameters.setPasswd(passWord);
                /*new LoginTask(LoginActivity.this,loginParameters,new BaseLiteners.ObjectResponseListener() {
                    @Override
                    public void OnSuccess(Object o) {
                        LoginResponse loginResponse = (LoginResponse)o;
                        LoginConfig loginConfig = LoginConfig.getLoginConfig();
                        loginConfig.setToken(loginResponse.getToken());
                        loginConfig.setShopMember(loginResponse.getShopMember());
                        LoginConfig.saveLoginConfig(loginConfig);
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void OnFail(BaseException e) {
                        showToast(e.getErrmsg());
                    }
                },true).execute();*/
               new BaseTask<LoginRequest,LoginResponse>(LoginActivity.this,LoginRequest.class,LoginResponse.class,loginParameters,new BaseLiteners.BaseResponseListener() {
                   @Override
                   public void OnFail(BaseException e) {
                       showToast(e.getErrmsg());
                   }

                   @Override
                   public void OnSuccess(BaseResponse o) {
                       LoginResponse loginResponse = (LoginResponse)o;
                       LoginConfig loginConfig = LoginConfig.getLoginConfig();
                       loginConfig.setToken(loginResponse.getToken());
                       loginConfig.setShopMember(loginResponse.getShopMember());
                       LoginConfig.saveLoginConfig(loginConfig);
                       Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                       startActivity(intent);
                       finish();
                   }
               },true).execute();



            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
